//
//  NSString+JJ.m
//  WOEWO
//
//  Created by huangjingjin on 2017/3/15.
//  Copyright © 2017年 BPMAC. All rights reserved.
//

#import "NSString+JJQuick.h"
@implementation NSString (JJQuick)
/// 获取url的参数
-(NSDictionary *)jj_urlStringParamDic {
    if(self.length==0) {
        return @{};
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //获取参数对
    NSArray*paramsArray = [self componentsSeparatedByString:@"&"];
    if(paramsArray.count >= 2) {
        for(NSInteger i =0; i < paramsArray.count; i++) {
            NSString *singleParamString = paramsArray[i];
            NSArray *singleParamSet = [singleParamString componentsSeparatedByString:@"="];
            if (singleParamSet.count == 2) {
                NSString *key = singleParamSet[0];
                NSString *value = singleParamSet[1];
                if(key.length > 0 || value.length > 0) {
                    [params setObject:value.length > 0 ? value : @"" forKey:key.length > 0 ? key : @""];
                }
            }
        }
    }else if(paramsArray.count==1) {
        //无 &。url只有?后一个参数
        NSString *singleParamString = paramsArray[0];
        NSArray*singleParamSet = [singleParamString componentsSeparatedByString:@"="];
        if(singleParamSet.count == 2) {
            NSString *key = singleParamSet[0];
            NSString *value = singleParamSet[1];
            if(key.length > 0 || value.length > 0) {
                [params setObject:value.length > 0 ? value : @"" forKey:key.length > 0 ? key : @""];
            }
        }
    }
    return params;
}

/// HTML
+(NSString *)jj_filterHTML:(NSString *)html{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    
    NSString * regEx = @"&nbsp";
    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    return html;
}

/// 获得千位分隔符字符串
+(NSString *)jj_partStringWithSeparator:(NSString *)num{
    if (num && num.length && ![num isEqual:[NSNull null]]) {
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setPositiveFormat:@",###.00;"];
        NSNumber *number = [NSNumber numberWithDouble:num.doubleValue];
        return [numberFormatter stringFromNumber:number];
    }
    return @"0.00";
}

/// 判断是否为整数
+ (BOOL)jj_isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

/// 判断是否为浮点形
+(BOOL)jj_isPureFloat:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return[scan scanFloat:&val] && [scan isAtEnd];
}
@end
